package mydictionary;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Administrator
 * 工具实现类：文件实现
 */
public class DictionaryDaoIO implements DictionaryDao {

  @Override
  public void save(MyDictionary myDictionary) {
    //把传入的参数 myDictionary对象写入文件 d:\\d.txt
    //字符缓冲流 一次写一行
    BufferedWriter bw = null;
    try {
      //追加文件
      bw = new BufferedWriter(new FileWriter("d:\\d.txt", true));
      bw.write(myDictionary.getId() + "--" + myDictionary.getEnglish() +
          "--" + myDictionary.getChinese() + "--" + myDictionary.getComments());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public MyDictionary query(String name) {
        /*根据传入的英文name，返回词典对象
        * 循环读取文本中的每一行文本
        *     比较（name & 文本）
        *     相同：返回对象*/
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader("d:\\d.txt"));
      String line;
      while ((line = br.readLine()) != null) {
        //读取的一行文本内容：
        String[] data = line.split("--");
        if (name.equals(data[1])) {
          int id = Integer.parseInt(data[0]);
          String english = data[1];
          String chinese = data[2];
          String comments = data[3];
          MyDictionary d = new MyDictionary(id, english, chinese, comments);
          return d;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return null;
  }

  @Override
  public void queryAll() {
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader("d:\\d.txt"));
      String line = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public void delete(String name) {
      /*判断要删除的内容是否存在
      * 若不存在提示不存在
      * 若存在
      *   循环操作
      *   读取原文件中的每一行文本
      *   比较是否是要删除的内容
      *   若不是，写入新文件
      * 最后，将原文件删除，新文件重命名
      * */

    if (query(name) == null) {
      System.out.println("要删除的内容不存在");
      //结束delete方法
      return;
    } else {
      BufferedWriter bw = null;
      BufferedReader br = null;
      try {
        //字符缓冲输入流，读取源文件的内容
        br = new BufferedReader(new FileReader("d:\\d.txt"));
        //字符缓冲输出流，写入新文件
        bw = new BufferedWriter(new FileWriter("d:\\new.txt", true));
        String line;
        //循环读取
        while ((line = br.readLine()) != null) {
          String[] data = line.split("--");
          if (!name.equals(data[1])) {
            bw.write(line);
            bw.newLine();
            bw.flush();
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (br != null) {
          try {
            br.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
          if (bw != null) {
            try {
              bw.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
    //最后，将原文件产出，新文件重命名
    File oldFile=new File("d:\\d.txt");
    File newFile=new File("d:\\new.txt");
    oldFile.delete();
    newFile.renameTo(oldFile);
    System.out.println("删除成功");

  }
}
